package com.example.yc23.demo.controller.fore;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yc23.demo.pojo.Film;
import com.example.yc23.demo.service.FilmService;
import com.example.yc23.demo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/fore/film")
public class ForeFilmController {
    @Autowired
    private FilmService filmService;

    @RequestMapping("/score")
    public JsonResult score(Integer filmId,Integer stars,String token){
        return filmService.score(filmId,stars,token);
    }
    @RequestMapping("/review")
    public JsonResult view(Integer filmId,String content,String token){
        return filmService.review(filmId,content,token);
    }
    /**
     * 根据类别检索电影列表
     * @param type 电影类别，默认为ALL
     * @return  以JSON格式返回电影列表数据
     */
    @RequestMapping("/list")
    public Page<Film> list(@RequestParam(defaultValue = "ALL") String type,
                           @RequestParam(defaultValue = "1") Integer pn,
                           @RequestParam(defaultValue = "5") Integer ps){
        QueryWrapper wrapper=null;
        if (!"ALL".equals(type)){
            wrapper=new QueryWrapper();
            wrapper.like("type",type);
        }
        Page page=new Page(pn,ps);
        return filmService.page(page,wrapper);
    }

    /**
     * 使用关键字进行模糊查询，根据电影名称和别名两个字段
     * @param word  电影名称关键字
     * @param pn    页号
     * @param ps    每页显示条数
     * @return  一个JSON对象包含电影列表和分页信息
     */
    @RequestMapping("/search")
    public Page<Film> search(@RequestParam(defaultValue = "ALL") String word,
                           @RequestParam(defaultValue = "1") Integer pn,
                           @RequestParam(defaultValue = "5") Integer ps){
        QueryWrapper wrapper=null;
        if (word!=null&&!"".equals(word)){
            wrapper=new QueryWrapper();
            wrapper.like("name",word);
            wrapper.or();
            wrapper.like("another_name",word);
        }
        Page page=new Page(pn,ps);
        return filmService.page(page,wrapper);
    }

    /**
     * 根据id查询电影详情
     * @param id FilmId
     * @return  电影对象
     */
    @RequestMapping("/detail")
    public Film detail(@RequestParam(required = true) Integer id){
        return filmService.getFilmDetail(id);
    }
}
